package thesis.filesharing.web.auth;

import javax.jdo.annotations.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import thesis.filesharing.bo.AuthBO;
import thesis.filesharing.common.exception.system.AppSystemException;

public class AuthenticationUserDetailsGetter implements UserDetailsService {
	
	@Autowired
	private AuthBO authBO;
	
	public AuthenticationUserDetailsGetter() {
		
	}
	
	@Override
	@Transactional
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		User user = null;
		try {
			user = authBO.findUserByUsername(username);
		} catch (AppSystemException ex) {
			ex.printStackTrace();
		}
		
		if (user == null) {
			throw new UsernameNotFoundException("User is not found");
		}
		
		return new AuthenticationUserDetails(user);
	}

}
